<template>
  <div class="my_person">
    <el-card style="width: 55%;">
      <el-form :model="user" ref="formRef" label-width="80px" :rules="rules">
        <el-form-item label="原始密码" prop="username">
          <el-input show-password v-model="user.password" disabled placeholder="请输入原始密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input show-password v-model="user.newPassword" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="comfirmPassword">
          <el-input show-password v-model="user.comfirmPassword" placeholder="请输入确认密码"></el-input>
        </el-form-item>
        <div style="text-align: center;margin-bottom: 20px;">
          <el-button type="primary" @click="update">确认修改</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>


<script>


export default {
  name: 'Password',
  data() {
    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
        ],
        comfirmPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    update() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
            this.user.password = this.user.newPassword
            //保存数据到数据库
            this.$request.put('/user/update', this.user).then(res => {
              if (res.code == 200) {
                this.$message.success('保存成功')
                //跳转登录页面
                this.$router.push('/login')
                
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      //更新数据
    }
  }
}
</script>

<style scope>
.my_person ::v-deep .el-form-item__label {
  font-weight: bold;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-radius: 50%;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 50%;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
  }
</style>